<template>
	<view class="card-body" :style="[AppSkinVar]">

	<!-- #ifdef H5 -->
 <view class="custom-box"></view>
 <!-- #endif -->
	<!-- #ifdef APP-PLUS -->
 <view class="custom-box" :style="{height: (pageType==='factory'? 100:statusBarHeight+190)+'rpx'}"></view>
	<!-- #endif -->
		<view class="cart-box">
			<!-- #ifdef H5 -->
			<view class="header-box bg-white" :style="{top: pageType==='factory'? statusBarHeight+44+'px': 0}" v-if="cartInfo.productAll&&isLogin" >
			<!-- #endif -->
			<!-- #ifdef APP-PLUS -->
			<view class="header-box bg-white" :style="{top: 0}" v-if="cartInfo.productAll&&isLogin" >
			<!-- #endif -->
				<view class="tabbar-status" v-if="pageType!=='factory'"></view>
				<view class="manage-box u-flex u-flex-between">
					<view class="u-font-26 c-b2">
						共
						<text class="c-primary">{{ cartInfo.productAll }}</text>
						件
					</view>
					<view class="btn" @tap="setManage">{{ isManage ? '完成' : '编辑' }}</view>
				</view>
			</view>
			<view class="m-t-20">
				<view class="goods-box" v-if="isLogin">
					<view class="u-m-b-20" v-for="(item, index) in cartInfo.data" :key="index">
						<view class="bg-white">
							<view>
								<view class="u-flex u-flex-between store-tlt1">
									<slot name="header">
										<view class="u-flex flex-1" style="position: relative;"
											@tap="skipStore(item.store)">
											<template>
												<u-checkbox-group @change="changeStoreCheck(item)">
													<u-checkbox class="u-m-r-28" shape="circle"
														:activeColor="radioColor" :checked="item.store.active"
														:disabled="item.store.disable"></u-checkbox>
												</u-checkbox-group>
											</template>

											<text class="w-600">{{ item.store.name }}</text>
											<text class="w-600 area-text" v-if="item.store&&item.store.storeTypeName"
												:style="{'background-color': areaColor[item.baskets[0].subareaId]}">{{ item.store.storeTypeName }}</text>
										</view>
									</slot>
								</view>
								<view v-for="(goods, gIndex) in item.baskets" :key="gIndex">
									<view class="goods-info u-flex align-initial goods-info1"
										@tap="$goTo('/pages/classify/goods/info/index', { id: goods.prodId || goods.commodityId })">
										<template>
											<view>
												<u-checkbox-group @change="changeGoodsCheck(item,goods)">
													<u-checkbox shape="circle" class="u-m-r-28" :checked="goods.active"
														:disabled="goods.disable"
														:activeColor="radioColor"></u-checkbox>
												</u-checkbox-group>
											</view>
										</template>

										<view class="right-box u-flex flex-1">
											<view class="u-m-r-20">
												<u-image mode="aspectFill" width="180rpx" height="180rpx"
													:src="$fullLink(getGoodsObj(goods, goodsKeys.image))">
													<u-loading-icon slot="loading"></u-loading-icon>
												</u-image>
											</view>
											<view
												class="goods-right u-flex align-initial u-flex-between flex-col flex-1">
												<view class="goods-tlt  u-m-b-10 u-flex u-flex-between align-initial">
													<view class="u-line-2 flex-1"
														:class="{ 'c-9a': isTextColorChange(goods) }">
														{{ getGoodsObj(goods, goodsKeys.name) }}
													</view>
												</view>

												<template>
													<view class="c-regular u-m-b-14">
														{{ getGoodsObj(goods, goodsKeys.skuName) }}
														<text class="u-font-20 c-red"
															v-if="goods.productSku&&goods.productSku.contributionValue">（积分：{{ goods.productSku.contributionValue}} ）</text>
													</view>
													<view class="u-flex u-flex-between">
														<view class="price c-red w-600">
															{{$unitEn}}{{ getGoodsObj(goods, goodsKeys.price) }}
														</view>
														<u-number-box bgColor="#fff" :disabledInput="true"
															color="#999999" :min="1"
															:max="goods.productSku ? goods.productSku.stock : 999999"
															v-model="goods.basketCount" :asyncChange="true"
															@change="editCount($event, goods, gIndex,goods)"></u-number-box>
													</view>
												</template>
											</view>
										</view>

									</view>

								</view>

							</view>
						</view>

					</view>
				</view>

				<view style="width: 100%;height: 70vh" class="flex flex-col flex-col-center flex-row-center" v-if="emptyShow">
					 <image style="width: 410rpx;height: 300rpx" src="http://saas.yanchaochao.com/mp-static/goods/empty-cart.png"></image>
					 <view style="color: #999999">{{isLogin ? '暂无商品' : '未登录'}}</view>
						<view class="button-box p-t-60" v-if="!isLogin">
								<u-button :color="AppSkinVar.primary" shape="circle" text="去登录" @click="login"></u-button>
						</view>
						<view class="button-box u-p-t-60" v-else>
								<u-button :color="AppSkinVar.primary" shape="circle" text="去逛逛"
													@click="goPage"></u-button>
						</view>
				</view>
			</view>
			<view v-if="cartInfo.invalid && cartInfo.invalid.length">
				<view>
					<view class="invalid-manage-box u-flex u-flex-between">
						<text>已失效商品</text>
						<view class="btn" @tap="setInvalidManage">{{ isInvalidManage ? '完成' : '编辑' }}
						</view>
					</view>
				</view>
				<view>
					<view class="goods-box">
						<invalid-goods :isRadio="isInvalidManage" :list="cartInfo.invalid" :invalidCheck="invalidCheck"
							@checkInvalid="checkInvalid"></invalid-goods>
					</view>
				</view>
			</view>
			<template v-if="cartInfo.productAll || (cartInfo.invalid && cartInfo.invalid.length)">
				<view class="recommend-title u-flex u-flex-center bg-white u-m-t-20 ">
					<view class="u-font-32 w-600 u-rela">猜您喜欢</view>
				</view>

				<goods-list class="u-m-t-20" :leftGoodsList="leftGoodsList" :rightGoodsList="rightGoodsList"
					:showLoad="showLoad" @getData="getGoodsData"></goods-list>
			</template>

			<view style="height: 236rpx;"></view>
			<view class="ios-btm"></view>
			<view class="btm-fixed-boxs" v-if="cartInfo.productAll || isInvalidManage">
				<view class="contxt-box u-flex u-flex-between bg-white" style="justify-content: flex-end;">
					<view class="right-box u-flex">
						<view class="txt-box" v-if="!isManage && !isInvalidManage">
							总计：
							<text class="c-primary">{{ $unitEn }}{{ countMoney }}</text>
						</view>
						<view class="right-box__button u-flex-xy-center" :class="{ 'is-ok': isActive }"
							@tap="$u.throttle(submit, 1000)">
							<text>
								{{ isManage || isInvalidManage ? '删除' : '结算'
								}}{{ !isManage && !isInvalidManage && settlementGoodsList.length ? `(${settlementGoodsList.length})` : '' }}
							</text>
						</view>
					</view>
				</view>
				<view style="height: 112rpx;"></view>
				<view class="ios-btm"></view>
			</view>
		</view>
		<!-- 规格弹窗 -->
		<sku-popup :info="reselectionGoodsInfo" :primary="AppSkinVar.primary" :checkInfo="reselectionCheckInfo"
			:show="popupShow" @confirm="skuConfirm" @close="() => (popupShow = false)"></sku-popup>
	</view>
</template>
<script>
	import {
		mapState,
		mapActions
	} from 'vuex'
	import {
		postDleCart,
		postEditCart
	} from '@/common/api/shoppingCart.js'

	import {
		getGoodsSkuDetail,
		getPreferenceGoodsList,
		getSubareaList
	} from '@/common/api/goods.js'

	import {	Debounce } from '@/common/utils/tools.js'
	import invalidGoods from './child/invalidGoods.vue'
	import skuPopup from './child/skuPopup.vue'
	import goodsList from './child/goods-list.vue'
	
	// 购买商品权限前的校验，包含一个verificationPurchasePermission的异步函数，套版项目可根据项目需求改动该文件实现验证购买权限
	import purchaseGoodsMixIn from '@/common/mixins/purchaseGoodsMixIn.js'
	// 所有一级页面均需引入skinMixIn，在页面的最顶级元素中设置css变量，skinMixIn包含AppSkin与AppSkinVar，AppSkin为当前设置的皮肤，AppSkinVar返回一个皮肤对象，对象内包含所有颜色的css变量
	import skinMixIn from '@/common/mixins/skinMixIn.js'

	export default {
		props: {
			onShow: Boolean,
			reachBottom: Boolean,
			downRefresh: Boolean,
			pageType: String,
      storeId: String
		},
		mixins: [skinMixIn, purchaseGoodsMixIn],
		components: {
			invalidGoods,
			skuPopup,
			goodsList
		},
		data() {
			return {
				isManage: false,
				goodsMoney: 0,
				goodsKeys: {
					price: 'productSku.price',
					image: 'productSku.skuCoverImg',
					name: 'productSku.productName',
					skuName: 'productSku.skuSpec',
					qry: 'qry'
				},
				isInvalidManage: false,
				invalidCheck: [],
				// 要修改的商品规格信息
				reselectionGoodsInfo: {
					productSkus: []
				},
				// 选中的规格信息
				reselectionCheckInfo: {},
				popupShow: false,
				// 修改规格的购物车商品id
				editId: null,
				showLoad: false,
				pageNum: 1,
				leftGoodsList: [],
				rightGoodsList: [],
				isGetGoods: true,
				// 分区
				areaList: [],
				areaColor: ['#20a53a', '#ffefae', '#8A2BE2', '#A52A2A', '#5F9EA0', '#7FFF00', '#D2691E', '#6db6ed','#DC143C', '#00FFFF'],
				currentStoreId: '',
				areaRadio: false,
				storeRadio: false,
				isActive: true,
				activeIds: [],
				payModeIds: [],
				countMoney: 0,
				statusBarHeight: 0
			}
		},
		computed: {
			...mapState({
				cartInfo: state => state.init.cartInfo,
				cartCheckMap: state => state.init.cartCheckMap,
				isLogin: state => state.user.isLogin
			}),
			radioColor() {
				return this.AppSkinVar.primary
			},
			cartCheck() {
				let list = []
				this.cartCheckMap.forEach(item => list.push(item))
				return list
			},
			settlementGoodsList() {
				return this.cartCheck.filter(item => item.productSku.stock > 0).map(item => item.id)
			},
			emptyShow() {
				if (!this.isLogin) {
					return true
				} else {
					return !this.cartInfo.productAll && this.cartInfo.invalid && this.cartInfo.invalid?.length == 0
				}
			},
			isOk() {
				if (this.isInvalidManage) {
					return !this.invalidCheck.length
				} else {
					return !this.cartCheckMap.size
				}
			},
			isAll() {
				let tempNow = []
				let allList = []
				if (this.isInvalidManage == false && this.cartInfo.productAll) {
					// 编辑正常商品时
					this.cartInfo?.data && this.cartInfo.data.map(bitem => {
						// 因为官方旗舰是属于爆品专区的商品，所以不能全选
						if (bitem.store.name == '官方旗舰') {

						} else {
							bitem.baskets.map(item => {
								allList.push(item.id)
								if (this.cartCheckMap.has(item.id)) {
									tempNow.push(item.id)
								}
							})
						}

					})
					return tempNow.length == allList.length && allList.length ? true : false
				} else {
					// 编辑失效商品时
					this.cartInfo?.invalid && this.cartInfo.invalid.map(item => {
						allList.push(item.id)
						if (this.invalidCheck.includes(item.id)) {
							tempNow.push(item.id)
						}
					})
					return tempNow.length == allList.length && allList.length ? true : false
				}
			}
		},
		watch: {
			onShow(newValue) {

				if (newValue) {
					// this.getCart().then(res => {
					// this.cartInfo.data.forEach(item => {
					// 	item.store['active'] = false
					// 	item.store['disable'] = false
					// 	item.baskets.forEach(goods => {
					// 		goods['active'] = false
					// 		goods['disable'] = false
					// 	})
					// })
					// this.getAreaList()
					// this.countMoney = 0
					// this.payModeIds = []
					// this.activeIds = []
					// this.isActive = true
					// console.log(this.cartInfo.data)
					// this.computedGoodsMoney()
					// })
				}
			},
			reachBottom(newValue) {
				if (newValue && this.onShow) {
					this.getGoodsData()
				}
			},
			downRefresh(newValue) {
				if (newValue && this.onShow) {
					if (this.isLogin) {
						this.pullDownRefresh()
					} else {
						this.$emit('endRefresh')
					}

				}
			}
		},

		created() {
			this.getGoodsData()
			this.computedGoodsMoney()
			this.getAreaList()
			uni.getSystemInfo({
				success(e) {
					this.statusBarHeight = e.statusBarHeight
				}
			})
		},

		methods: {
			...mapActions(['getCart']),

            goPage(){
                if(this.pageType === 'factory'){
                    this.$goTo('/pages/factory/index?id='+this.storeId)
                    return
                }
                uni.setStorageSync('tabbarPageIndex',1)
                this.$switchTab('/pages/tabbar/index')
            },

			// 编辑
			editCount(e, goods, index) {
				// if(productSku)
				goods.basketCount = e.value
				// console.log(goods)

				console.log(goods)

				// return
				this.newEditCount(e.value, goods.id)
			},

			// 勾选店铺
			changeStoreCheck(item) {
				item.store.active = item.store.active ? false : true

				item.baskets.forEach(bask => {
					bask.active = item.store.active
				})

				this.setCheckValue()
			},

			// 勾选商品
			changeGoodsCheck(item, goods) {

				goods.active = goods.active ? false : true

				// 此店铺下的商品是否全部都被选中了
				let allChoose = false
				let isChoose = false
				item.baskets.forEach(bask => {
					if (!bask.active) {
						allChoose = true
					} else {
						isChoose = true
					}
				})

				// 全部被选中，那么店铺的checkbox也要被选中
				this.$set(item.store, 'active', allChoose ? false : true)

				this.setCheckValue(item, goods)

			},

			setCheckValue(item, goods) {

				let list = this.cartInfo.data
				console.log(2222, list, item, goods);
				let currentSubareaId = ''

				for (let i = 0; i < list.length; i++) {
					list[i].baskets.forEach(bask => {
						if (bask.active) {

							currentSubareaId = bask.subareaId
							console.log(888, currentSubareaId)
						}
					})
				}
				console.log(999, currentSubareaId);
				if (currentSubareaId == '') {
					for (let i = 0; i < list.length; i++) {
						list[i].baskets.forEach(bask => {
							list[i].store.disable = false
							bask.disable = false
						})
					}
				} else {
					// 非编辑状态下，计算checkbox是否可勾选
					if (!this.isManage) {
						console.log(4324343434)
						for (let i = 0; i < list.length; i++) {
							list[i].baskets.forEach(bask => {
								if (bask.subareaId != currentSubareaId) {
									list[i].store.disable = true
									bask.disable = true
								}
							})
						}
					}
				}

				// 勾选商品的id
				let isActive = true
				let activeIds = []
				let payModeIds = []
				let countMoney = 0
				this.cartInfo.data.forEach(cart => {
					cart.baskets.forEach(bask => {
						if (bask.active && !bask.disable) {
							isActive = false
							activeIds.push(bask.id)

							// countMoney += bask.productSku.price * bask.basketCount
							countMoney = this.$accAdd( countMoney, this.$accMul(bask.productSku.price, bask.basketCount))
							if (bask.payModeId != '' && bask.payModeId != null) {
								console.log(222, bask.payModeId);
								payModeIds.push(bask.payModeId.split(","))
							}
						}
					})
				})

				this.isActive = isActive
				this.activeIds = activeIds
				let ids = this.intersection(payModeIds)
				this.payModeIds = ids
				this.countMoney = countMoney
			},

			getAreaList() {
				getSubareaList().then(res => {
					if (res.code == 200) {
						this.areaList = res.data
						this.areaList.push({
							id: '0',
							name: ' '
						})
						if (this.cartInfo.data != undefined) {
							this.cartInfo.data.forEach(cart => {
								this.areaList.forEach(area => {
									if (area.id == cart.baskets[0].subareaId) {
										this.$set(cart.store, 'storeTypeName', area.name)
									}
								})
							})
						}

					}
				})
			},

			isTextColorChange(goods) {
				return (goods.productSku && goods.productSku.stock === 0)
			},

			// 回显商品信息
			getGoodsObj(goods, key) {
				let keyArray = key.split('.')
				let value = goods
				keyArray.forEach(v => {
					value = value[v]
				})
				return value
			},

			// 点击跳转店铺
			skipStore(store) {
				if (store.type == 1) {
					this.$goTo('/pages/store/index', {
						id: store.id
					})
				} else {
					this.$goTo('/pages/nearby/store/info', {
						id: store.id
					})
				}
			},

			getGoodsData() {
				if (this.isGetGoods && !this.showLoad) {
					this.showLoad = true
					getPreferenceGoodsList({
						pageSize: 10,
						pageNum: this.pageNum
					}).then(res => {
						this.showLoad = false
						if (res.code === 200) {
							let pageNum = this.$waterfallsPageLoad(this.leftGoodsList, this.rightGoodsList, res.rows, this.pageNum, 10)
							this.pageNum = pageNum
							if (this.leftGoodsList.length + this.rightGoodsList.length == res.total) {
								this.isGetGoods = false
							}
						}
						this.$emit('setReachBottom')
					})
				}
			},
			pullDownRefresh() {
				this.pageNum = 1
				this.isGetGoods = true
				this.showLoad = false
				this.goodsList = []
				this.getGoodsData()
				this.getCart(this.storeId)
				this.$emit('endRefresh')
			},
			login() {
				this.$goLogin({
					isShowModal: false
				})
			},
			setManage() {
				this.isManage = !this.isManage
				this.isInvalidManage = false

				// this.cartInfo.data.forEach(cart => {
				//   this.$set(cart.store, "disable", false)
				//   this.$set(cart.store, "active", false)
				//   cart.baskets.forEach(bask => {
				//     this.$set(bask, "disable", false)
				//     this.$set(bask, "active", false)
				//   })
				// })

			},
			setInvalidManage() {
				this.isInvalidManage = !this.isInvalidManage
				this.isManage = false
			},
			checkAll() {
				this.areaRadio = true
				this.storeRadio = true
				this.areaRadio = this.areaRadio ? false : true
				this.storeRadio = this.storeRadio ? false : true
				let tempNow = []
				let allList = []
				if (this.isInvalidManage) {
					// 失效商品全选
					let tempList = this.invalidCheck
					this.cartInfo?.invalid &&
						this.cartInfo.invalid.map(item => {
							allList.push(item.id)
							if (this.invalidCheck.includes(item.id)) {
								tempNow.push(item.id)
							}
						})
					if (tempNow.length == allList.length) {
						tempList = []
					} else {
						this.cartInfo.invalid.map(item => {
							if (!this.invalidCheck.includes(item.id)) {
								tempList.push(item.id)
							}
						})
					}
					this.invalidCheck = tempList
				} else {
					let tempList = new Map(this.cartCheckMap)
					this.cartInfo?.data && this.cartInfo.data.map(bitem => {
						// 因为官方旗舰是属于爆品专区的商品，所以不能全选
						if (bitem.store.name == '官方旗舰') {

						} else {
							bitem.baskets.map(item => {
								allList.push(item.id)
								if (this.cartCheckMap.has(item.id)) {
									tempNow.push(item.id)
								}
							})
						}

					})
					if (tempNow.length == allList.length) {
						this.$store.commit('setCartCheck', new Map([]))
						this.goodsMoney = 0
					} else {
						this.cartInfo.data.map(bitem => {
							bitem.baskets.map(item => {
								if (bitem.store.name == '官方旗舰') {

								} else {
									if (!this.cartCheckMap.has(item.id)) {
										tempList.set(item.id, item)
										if (this.isNormalGoods(item)) {
											this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(
												item
												.productSku.price, item.basketCount))
										}
									}
								}

							})
						})
						this.$store.commit('setCartCheck', tempList)
					}
					this.getCart(this.storeId)
				}
			},
			// 判断一个商品是否为正常可结算的商品
			isNormalGoods(goods) {
				return goods.productSku.stock > 0
			},
			// 购物车内不为失效状态的店铺的勾选、取消勾选
			checkStore(list) {

				let isArea = false // 是否勾选了爆品专区的商品
				for (let [key, value] of this.cartCheckMap) {
					if (value.storeId == this.currentStoreId) {
						isArea = true
					}
				}

				list.forEach(item => {
					if (item.storeId == this.currentStoreId) {
						isArea = true
					}
				})

				if (isArea) {
					this.storeRadio = this.storeRadio ? false : true
					this.areaRadio = false
				} else {
					this.areaRadio = this.areaRadio ? false : true
					this.storeRadio = false
				}

				console.log(list)

				// console.log(list)
				// console.log(this.cartInfo)
				let tempList = new Map(this.cartCheckMap)
				let tempNow = []
				list.map(item => {
					if (this.cartCheckMap.has(item.id)) {
						tempNow.push(item)
					}
				})
				if (tempNow.length == list.length) {
					tempNow.map(item => {
						tempList.delete(item.id)
						if (this.isNormalGoods(item)) {
							this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(-item.productSku.price,
								item.basketCount))
						}
					})
				} else {
					list.map(item => {
						if (!this.cartCheckMap.has(item.id)) {
							tempList.set(item.id, item)
							if (this.isNormalGoods(item)) {
								this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(item.productSku.price,
									item.basketCount))
							}
						}
					})
				}
				// console.log([...tempList.entries()])

				// console.log(tempNow)
				this.$store.commit('setCartCheck', tempList)
				// this.getCart()
			},
			// 购物车内不为失效状态的商品的勾选、取消勾选
			checkCart(goods) {
				let tempList = new Map(this.cartCheckMap)
				if (this.cartCheckMap.has(goods.id)) {
					tempList.delete(goods.id)
					if (this.isNormalGoods(goods)) {
						this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(-goods.productSku.price, goods
							.basketCount))
					}
				} else {
					tempList.set(goods.id, goods)
					if (this.isNormalGoods(goods)) {
						this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(goods.productSku.price, goods
							.basketCount))
					}
				}
				this.$store.commit('setCartCheck', tempList)
				this.getCart(this.storeId)
			},
			// 购物车内为失效状态的商品的勾选、取消勾选
			checkInvalid(id) {
				let tempList = this.invalidCheck
				if (this.invalidCheck.find(o => o == id)) {
					tempList.splice(tempList.indexOf(id), 1)
				} else {
					tempList.push(id)
				}
				this.invalidCheck = tempList
			},
			reselection(productId, id) {
				this.editId = id
				getGoodsSkuDetail({
					productId
				}).then(res => {
					if (res.code == 200) {
						this.reselectionGoodsInfo = res.data
						this.popupShow = true
					} else {
						uni.showToast({
							icon: 'none',
							title:res.msg
						})
					}
				})
			},
			// 修改购物车商品规格
			skuConfirm(basketCount) {
				this.popupShow = false
				postEditCart({
						skuId: this.reselectionCheckInfo.id,
						basketCount
					},
					this.editId
				).then(res => {
					this.getCart(this.storeId).then(res => {
						if (res.code == 200) {
							this.computedGoodsMoney()
						}
					})
				})
			},
			computedGoodsMoney() {
				this.goodsMoney = 0
				this.cartInfo?.data &&
					this.cartInfo.data.map(bitem => {
						bitem.baskets.map(item => {
							if (this.cartCheckMap.has(item.id)) {
								this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(item.productSku
									.price, item.basketCount))
							}
						})
					})
			},
			newEditCount: Debounce(function(basketCount, id) {
				postEditCart({
						basketCount
					},
					id
				).then(res => {
					this.setCheckValue()
				})
			}, 400),
			recalculate(res) {
				if (res.code == 200) {
					this.goodsMoney = 0
					let tempList = new Map([])
					this.cartInfo?.data &&
						this.cartInfo.data.map(bitem => {
							bitem.baskets.map(item => {
								if (this.cartCheckMap.has(item.id)) {
									this.goodsMoney = this.$accAdd(this.goodsMoney, this.$accMul(item
										.productSku.price, item.basketCount))
									tempList.set(item.id, item)
								}
							})
						})
					this.$store.commit('setCartCheck', tempList)
					this.$forceUpdate()
				}
			},
			submit() {
				if (this.isInvalidManage) {
					this.$showModal({
						content: '确定删除选中的失效商品?',
						success: res => {
							if (res.confirm) {
								this.del(this.invalidCheck.join(','))
							}
						}
					})
				} else {
					if (this.activeIds.length == 0) {
						uni.showToast({
							icon: 'none',
							title: '请勾选需要购买的商品!'
						})
						return
					}
					if (this.isManage) {
						let ids = this.cartCheck.map(item => item.id).join(',')
						//删除
						this.del(this.activeIds.join(','))
					} else {
						//结算
						// 判断是否存在库存不足的商品 即购买量大于库存
						// let noStock = false
						// this.cartCheck.some(item => {
						// 	if (item.basketCount > item.productSku.stock) {
						// 		noStock = true
						// 		return noStock
						// 	}
						// })
						// if (noStock) {
						// uni.showToast({
						// 	icon: 'none',
						// 	title: '库存不足!'
						// })
						// 	return
						// }
						this.verificationPurchasePermission().then(res => {
							if (res.code === 200) {
								// console.log(this.settlementGoodsList)
								// console.log(this.cartInfo.data)
								// let ids = []
								// this.cartInfo.data.forEach(cart => {
								// 	cart.baskets.forEach(bask => {
								// 		this.settlementGoodsList.forEach(sett => {
								// 			if (sett == bask.id && bask.payModeId != '') {
								// 				ids.push(bask.payModeId)
								// 			}
								// 		})
								// 	})
								// })
								// console.log(ids)
								// let payModeId = this.unLink(ids)
								// console.log(payModeId)
								this.$goTo('/pages/classify/goods/previewOrder', true, {
									type: 2,
									basketIds: this.activeIds.join(','),
									payModeId: this.payModeIds.join(','),
									showAddress: 'cart'
								})
							}
						})
					}
				}
			},

			// 取交集
			intersection(arr) {
				let minArr = [],
					defaultArr = [];
				arr.forEach(i => {
					minArr = i
					if (i.length < minArr.length) {
						minArr = i
					}
				})
				minArr.forEach(i => {
					let state = []
					arr.forEach(j => {
						if (j.includes(i)) {
							state.push(true)
						} else state.push(false)
					})
					const status = state.every(i => {
						return i === true
					});
					status && !defaultArr.includes(i) && defaultArr.push(i);
				})
				return defaultArr
			},

			// 去重
			unLink(arr) {
				return arr.filter(function(item, index, arr) {
					return arr.indexOf(item, 0) === index
				})
			},

			del(ids) {
				postDleCart(ids).then(res => {
					if (res.code == 200) {
						this.getCart(this.storeId).then(res => {

							this.cartInfo.data.forEach(item => {
								item.store['active'] = false
								item.store['disable'] = false
								item.baskets.forEach(goods => {
									goods['active'] = false
									goods['disable'] = false
								})
							})

							this.getAreaList()

							this.countMoney = 0
							this.payModeIds = []
							this.activeIds = []
							this.isActive = true

							this.computedGoodsMoney()
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				})
				this.isInvalidManage = false
			}
		}
	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #f7f7f7 !important;
	}
	.c-b2 {
		color: #b2b2b2;
	}

	.header-box {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		z-index: 999;
	}

	.invalid-manage-box,
	.manage-box {
    height: 90rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-end !important;
		font-size: 30rpx;
		padding-left: 40rpx;
		padding-right: 40rpx;
		padding-bottom: 16rpx;
		.btn {
			color: #4d4d4d;
		}
	}

	.btm-fixed-boxs {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background: #ffffff;
		z-index: 10;
		.contxt-box {
			padding: 16rpx 30rpx;
			.left-box {
				font-size: 30rpx;
			}

			.right-box {
				.txt-box {
					font-size: 30rpx;
					line-height: 42rpx;
				}

				&__button {
					width: 218rpx;
					height: 80rpx;
					color: #ffffff;
					font-size: 30rpx;
					background: var(--color-primary);
					border-radius: 40rpx;
					margin-left: 20rpx;
				}
			}
		}
	}

	.goods-box {
		padding: 0 20rpx;
		>view {
			border-radius: 10rpx;
			overflow: hidden;
		}
	}

	.recommend-title {
		padding: 24rpx 0;
		view {
			padding: 0 68rpx;
			color: #303133;
			&::after,
			&::before {
				content: '';
				position: absolute;
				width: 68rpx;
				height: 2rpx;
				background-color: var(--color-primary);
				top: 50%;
			}

			&::after {
				left: -32rpx;
			}

			&::before {
				right: -32rpx;
			}
		}
	}

	.button-box {
		width: 278rpx;
		margin: 0 auto;
	}

	.scale-8 {
		transform: scale(0.8);
	}

	.c-47 {
		color: #474f59;
	}

	.c-9a {
		color: #9a9a9a;
	}

	.border-10 {
		border-radius: 10rpx;
	}

	.store-tlt1 {
		padding: 34rpx 0rpx 8rpx 20rpx;
	}

	.store-tlt2 {
		padding: 16rpx 30rpx 16rpx 30rpx;
	}

	.goods-info {
		box-shadow: 0px -2rpx 0px 0px #f3f3f3;
	}

	.goods-info1 {
		padding: 30rpx 24rpx 30rpx 20rpx;
	}

	.goods-info2 {
		padding: 30rpx 30rpx 30rpx 30rpx;
	}

	.right-box {
		.goods-right {
			height: 100%;
			.goods-tlt {
				color: #454545;
				line-height: 40rpx;
				height: 80rpx;
			}
			.price {
				font-size: 32rpx;
				line-height: 38rpx;
			}
		}
	}

	/deep/.u-number-box {
		>view,
		input {
			border: 1rpx solid #cfcfcf;
			&:first-child {
				border-right: none;
			}
			&:last-child {
				border-left: none;
			}
		}
		input {
			width: 76rpx !important;
		}
	}
	.reselection {
		width: 94rpx;
		height: 54rpx;
		border-radius: 40rpx;
		border: 2rpx solid var(--color-primary);
		line-height: 50rpx;
	}
	.area-text {
		position: absolute;
		right: 30rpx;
		color: white;
		height: 52rpx;
		line-height: 52rpx;
		text-align: center;
		border-radius: 100rpx;
		padding: 4rpx 20rpx;
		background-color: #f5222d;
		margin-bottom: 20rpx;
	}
    .custom-box{
        height: calc(var(--status-bar-height) + 90rpx);
    }
	.tabbar-status{
		width: 100%;
		height: var(--status-bar-height);
	}
</style>
